function useFormat() {
    /**
     * 将文件大小格式化处理
     *
     * @param size 1024000000
     * @returns {string} "976.56MB"
     */
    const formatFileSize = (size) => {
        let fsText;

        if (size < 1048576) fsText = (size / 1024).toFixed(2) + 'KB';
        else if (size === 1048576) fsText = '1MB';
        else if (size > 1048576 && size < 1073741824) fsText = (size / (1024 * 1024)).toFixed(2) + 'MB';
        else if (size > 1048576 && size === 1073741824) fsText = '1GB';
        else if (size > 1073741824 && size < 1099511627776) {
            fsText = (size / (1024 * 1024 * 1024)).toFixed(2) + 'GB';
        } else fsText = '1TB+';

        return fsText;
    };

    /**
     * 将社区时间格式化处理
     *
     * @param time 任意时间戳 1636600271000
     * @returns {string} "2021年11月11日" | "11月11日" | "昨天" | "2小时前" | "30分钟前" | "刚刚"
     */
    const formatTime = (time) => {
        let date = new Date(time);
        let y = date.getFullYear(),
            m = date.getMonth() + 1,
            d = date.getDate();

        let nowDate = new Date();
        let nowY = nowDate.getFullYear();
        let nowTime = nowDate.getTime();

        let diff = nowTime - time;
        let dd = Math.floor(diff / (1000 * 60 * 60 * 24));
        let dh = Math.floor(diff / (1000 * 60 * 60));
        let dm = Math.floor(diff / (1000 * 60));

        if (nowY > y) return y + '年' + m + '月' + d + '日';
        else if (Math.floor(dd) > 1) return m + '月' + d + '日';
        else if (dd === 1) return '昨天';
        else if (dh >= 1) return '' + dh + '小时前';
        else if (dm >= 5) return '' + dm + '分钟前';
        else return '刚刚';
    };

    return {
        formatFileSize,
        formatTime,
    };
}

export default useFormat;
